package com.inmaytide.website.mapper.deploy.provider;

import com.inmaytide.website.domain.vo.deploy.DeployInfoVo;
import com.inmaytide.website.utils.pagination.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author Moss
 * @version 1.0.0
 * @since May 22, 2016
 */
public class DeployInfoMapperSQLProvider {

    public static final String TABLE_NAME = "deploy_info";

    private void setConditions(SQL sql, Map<String, String> condition) {
        if (condition == null || condition.isEmpty())
            return;

        if (StringUtils.isNotEmpty(condition.get("projectId"))) {
            sql.WHERE(String.format("a.project_id = %d", Long.valueOf(condition.get("projectId").toString())));
        }
        if (StringUtils.isNotEmpty(condition.get("content"))) {
            sql.WHERE(String.format("description like '%%%s%%'", condition.get("content")));
        }
        if (StringUtils.isNotEmpty(condition.get("optime_begin"))) {
            sql.WHERE(String.format("operating_time >= '%s'", condition.get("optime_begin") + " 00:00:00"));
        }
        if (StringUtils.isNotEmpty(condition.get("optime_end"))) {
            sql.WHERE(String.format("operating_time <= '%s'", condition.get("optime_end") + " 23:59:59"));
        }
    }

    @SuppressWarnings("unchecked")
    public String findCountByCondition(Map<String, Object> parameters) {
        Map<String, String> condition = null;
        if (parameters.get("condition") != null)
            condition = (Map<String, String>) parameters.get("condition");

        // 获取sql
        SQL sql = new SQL();
        sql.SELECT("count(a.id)");
        sql.FROM(TABLE_NAME + " a");
        setConditions(sql, condition);
        return sql.toString();
    }

    @SuppressWarnings("unchecked")
    public String findListByPagination(Map<String, Object> parameters) {
        Pagination<DeployInfoVo> pagination = (Pagination<DeployInfoVo>) parameters.get("pagination");
        Map<String, String> condition = (Map<String, String>) pagination.getConditions();

        SQL sql = new SQL();
        sql.SELECT("a.*, u.name as operate_user_name");
        sql.FROM(TABLE_NAME + " a");
        sql.LEFT_OUTER_JOIN("sys_user u on u.id = a.operate_user");
        setConditions(sql, condition);
        sql.ORDER_BY("a.id desc");

        // 组织分页SQL--ORACLE
//        StringBuffer sqlBuf = new StringBuffer();
//        sqlBuf.append(" SELECT *");
//        sqlBuf.append(" FROM");
//        sqlBuf.append("   (SELECT A.*,");
//        sqlBuf.append("     ROWNUM RN");
//        sqlBuf.append("   FROM ( " + sql.toString() + " ) A");
//        sqlBuf.append("   WHERE ROWNUM <= " + (pagination.getOffset() + pagination.getPageSize()));
//        sqlBuf.append("   )");
//        sqlBuf.append(" WHERE RN > " + pagination.getOffset());

        // 组织分页SQL--SQLITE
        StringBuffer sqlBuf = new StringBuffer(sql.toString() + " limit " + pagination.getPageSize() + " offset "
                + pagination.getOffset());
        return sqlBuf.toString();
    }

}
